package apis

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zorm-learn/app/dm/service"
	"zorm-learn/app/util"
)

var Routers = make([]func(*gin.RouterGroup), 0)

func init() {
	Routers = append(Routers, func(group *gin.RouterGroup) {
		routerGroup := group.Group("/dm")
		//	routerGroup.Use(util.LoginRequired())
		routerGroup.GET("/query", queryById)
		routerGroup.GET("/ping", pong)
		routerGroup.GET("/long", longText)
	})
}

func longText(context *gin.Context) {
	ctx, _ := util.MainCtx.DM.BindContextDBConnection(context)
	id, _ := context.GetQuery("id")
	data, err := service.FindTestLongStrStructById(ctx, id)
	if err != nil {
		context.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()})
		return
	}
	context.JSON(http.StatusOK, gin.H{"msg": "success", "data": data})
}

func pong(context *gin.Context) {
	context.JSON(http.StatusOK, gin.H{"msg": "success", "data": "pong"})
}
func queryById(context *gin.Context) {
	ctx, _ := util.MainCtx.DM.BindContextDBConnection(context)
	id, _ := context.GetQuery("id")
	data, err := service.FindTestableStructById(ctx, id)
	if err != nil {
		context.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()})
		return
	}
	context.JSON(http.StatusOK, gin.H{"msg": "success", "data": data})
}
